<?php
/**
 * Description: 重置密码的功能
 * User: devforma
 * Date: 15/2/3
 * Time: 10:44
 */

require LIB_DIR.'/phpass.class.php';

class ResetPassword implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
        if (empty($queryArgs['mobile_num']) || !validatePhone($queryArgs['mobile_num']) || empty($queryArgs['password'])) {
            return genOutput(1000);
        }

        $number =  trim($queryArgs['mobile_num']);
        $memKey = 'phone:'.$number;
        if (($value = cacheGet($memKey)) && $value['validated']) {
            $sql = "SELECT `ID` FROM `{$uddb->userAuthInfo}` WHERE `phone`=:phone";
            $userId = $uddb->getVar($sql, array(':phone' => $queryArgs['mobile_num']));

            if (empty($userId)) {
                return genOutput(1000);
            }

            $password = passDecrypt(trim($queryArgs['password']), ENCRYPTION_KEY);

            $phpass = new PasswordHash(8, true);
            $uddb->update($uddb->userAuthInfo,
                              array('user_pass' => $phpass->HashPassword($password)),
                              array('ID' => $userId));
            
            cacheDelete($memKey);

            return genOutput(1);
        }
        
        return genOutput(1103);
    }
}